{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from wavelet_denoising import wavelet_denoising\n",
    "from utils import *\n",
    "from heart_sound_segmentation2 import *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\numpy\\core\\fromnumeric.py:3622: RuntimeWarning: Degrees of freedom <= 0 for slice\n",
      "  **kwargs)\n",
      "D:\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\numpy\\core\\_methods.py:195: RuntimeWarning: invalid value encountered in true_divide\n",
      "  arrmean, rcount, out=arrmean, casting='unsafe', subok=False)\n",
      "D:\\Anaconda3\\envs\\pytorch\\lib\\site-packages\\numpy\\core\\_methods.py:226: RuntimeWarning: invalid value encountered in double_scalars\n",
      "  ret = ret.dtype.type(ret / rcount)\n"
     ]
    }
   ],
   "source": [
    "file_path = \"../../media/3f07c038b6bfd1900d7d792396c2baa6.wav\"\n",
    "second = 4\n",
    "downsampling_ratio = 2\n",
    "\n",
    "wave_data, wave_time, framerate = read_part_wavefile(file_path, second, downsampling_ratio)\n",
    "filteredData = butterworth_bandpass_filtering(wave_data, framerate, downsampling_ratio)\n",
    "denoisedData, usecoeffs = wavelet_denoising(filteredData, 'db4')\n",
    "standard_data = StandardScaler(denoisedData)\n",
    "normalized_data = MaxMinNormalization(standard_data)\n",
    "timesE, envelope = find_envelope(normalized_data, downsampling_ratio, framerate)\n",
    "intervals, maximum = envelope_segmentation(timesE, envelope, second)  # time为时间轴，envelope为包络值\n",
    "state_data = get_label(intervals, timesE, wave_time, downsampling_ratio, framerate)\n",
    "\n",
    "# 新建 amplitude.csv & label.csv\n",
    "amplitude_path = \"./csvs/amplitude.csv\"\n",
    "label_path = \"./csvs/label.csv\""
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0.13385668,  0.16380386,  0.17007745, ..., -0.37973962,\n        -0.15861741,  0.11665629]])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amplitude_array = np.array([normalized_data])\n",
    "amplitude_array"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "       0         1         2         3         4        5         6     \\\n0  0.133857  0.163804  0.170077  0.159736  0.161516  0.16786  0.177377   \n\n       7         8         9     ...      3990      3991      3992      3993  \\\n0  0.194007  0.207452  0.218159  ... -0.089628 -0.215907 -0.339044 -0.460318   \n\n       3994      3995      3996     3997      3998      3999  \n0 -0.541821 -0.572369 -0.524924 -0.37974 -0.158617  0.116656  \n\n[1 rows x 4000 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n      <th>6</th>\n      <th>7</th>\n      <th>8</th>\n      <th>9</th>\n      <th>...</th>\n      <th>3990</th>\n      <th>3991</th>\n      <th>3992</th>\n      <th>3993</th>\n      <th>3994</th>\n      <th>3995</th>\n      <th>3996</th>\n      <th>3997</th>\n      <th>3998</th>\n      <th>3999</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.133857</td>\n      <td>0.163804</td>\n      <td>0.170077</td>\n      <td>0.159736</td>\n      <td>0.161516</td>\n      <td>0.16786</td>\n      <td>0.177377</td>\n      <td>0.194007</td>\n      <td>0.207452</td>\n      <td>0.218159</td>\n      <td>...</td>\n      <td>-0.089628</td>\n      <td>-0.215907</td>\n      <td>-0.339044</td>\n      <td>-0.460318</td>\n      <td>-0.541821</td>\n      <td>-0.572369</td>\n      <td>-0.524924</td>\n      <td>-0.37974</td>\n      <td>-0.158617</td>\n      <td>0.116656</td>\n    </tr>\n  </tbody>\n</table>\n<p>1 rows × 4000 columns</p>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amplitude_df = pd.DataFrame(amplitude_array)\n",
    "amplitude_df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "amplitude_df.to_csv(amplitude_path, index_label=False, index=False, header=False)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}